# -*- coding: utf-8 -*-
# @Time : 2024/6/7 9:25 AM 
# @Author : Wayne
# @Email : 1
# @File : read_file_to_json_map.py
# @Project : young_python_demo
# @DESCRIPTION :

import json


def read_and_process_file(filename,  keys_to_keep=None):
    result = []
    # 如果 keys_to_keep 为空，则设置它为 None，以便稍后使用 item 的所有键
    if not keys_to_keep:
        keys_to_keep = None
    # 读取文件
    with open(filename, 'r') as file:
        content = file.read()

        # 将内容解析为 Python 对象

        data = json.loads(content)

        data = data.get("rows")

        # 处理数据，只保留特定的键值对或所有键值对

        for item in data:

            if keys_to_keep is None:

                # 如果没有指定要保留的键，则保留所有键值对

                result.append(item)

            else:

                # 否则，只保留指定的键值对

                filtered_item = {key: item[key] for key in keys_to_keep if key in item}

                result.append(filtered_item)


    return result


# 使用函数
filename = 'aa.txt'  # 你的文件名
# keys_to_keep = ["id", "itemCode", "itemName", "pjCode", "pjName", "upCode", "upName"]  # 你想要保留的键
keys_to_keep = []  # 你想要保留的键

new_data = read_and_process_file(filename, keys_to_keep)
# print(new_data)
table_name = "customer_midd_data.st_car_config_upd_goods"
insert_statements = []


for item in new_data:

    keys = ', '.join(item.keys())

    values = ', '.join(f"'{str(v)}'" for v in item.values())

    insert_statement = f"INSERT INTO items ({keys}) VALUES ({values});"

    insert_statements.append(insert_statement)


# 输出所有的插入语句

for statement in insert_statements:

    print(statement)